<template>
	<view>
		<view class="title">
			当前学期 
			<span style="border: 1rpx solid #ccc;padding: 10rpx;">
				{{this.classInfo.currentTermName}}
			</span>
		</view>
		<view class="class">
			<view class="class_item" :class="selList.id===item.id ? 'active':''" v-for="item of classList" @click="classItemClick(item)">
				<view>{{item.title}}</view>
				<view>{{item.classGoTime}}</view>
			</view>
		</view>
		<view class="btn">
			<u-button type="primary" @click="submit">确定</u-button>
			<u-button @click="cancel">取消</u-button>
		</view>
	</view>
</template>

<script>
import { data } from '../../uni_modules/uview-ui/libs/mixin/mixin'
	export default {
		data(){
			return{
				classInfo:{},
				classList:[],
				selList:{},
				num:null,
				xh:null,
				onOff:false,
				oldClassId:'',
			}
		},
		onLoad(e) {
			this.num = e.num
			this.xh = e.xh
			this.onOff = e.onOff
			this.oldClassId = e.oldClassId
			console.log(`第${e.num}课程`);
			this.getClass()
		},
		methods:{
			cancel(){
				uni.navigateBack(-1)
			},
			getLocalTime(nS) {
			    return new Date(parseInt(nS)).toLocaleString().replace(/:\d  {1,2}$/,' ');
			},
			submit(){
				if(!this.selList.id) return uni.$u.toast('请选择课程')
				// if(!this.selList.id) this.selList.id = ''
				let ApiName = !+this.onOff ? 'selectClass' : 'updateSelectClass'
				
				let val = {
					classId:this.selList.id,
					zyxh:this.num,
					xh:this.xh
				}
				if(ApiName === 'updateSelectClass'){
					val.oldClassId = this.oldClassId
				}
				this.$post(this.$api[ApiName],val,res=>{
					if(res.code === 200){
						uni.redirectTo({
							url:`/pages/index/signUp?num=${this.num}&title=${this.selList.title}&money=${res.data}&xh=${this.xh}`
						})
						const num = Date.parse(new Date())
						const outTime = this.getLocalTime(num)
						console.log(outTime);
						uni.setStorageSync('outTime', outTime)
					}else{
						uni.$u.toast(res.msg ? res.msg : '选课程失败！');
					}
				})
			},
			classItemClick(val){
				if(this.selList.id===val.id){
					this.selList = {}
				}else{
					this.selList=val
				}
			},
			getClass(){
				this.$post(this.$api.classInfoList,{},res=>{
					if(res.code === 200){
						console.log(res.data);
						this.classInfo = res.data
						this.classList = res.data.classInfoList
					}else{
						uni.$u.toast('获取课程失败！');
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.title{
		margin:20rpx;
	}
	.class{
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		.class_item{
			margin: 10rpx;
			height: 100rpx;
			display: flex;
			font-size: 26rpx;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			min-width: 300rpx;
			background-color: #eee;
			border-radius: 10rpx;
		}
		.active{
			background-color: #07c160;
			color: #fff;
		}
	}
</style>